﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DA
{
    // chi tiết tờ bướm
    public class ChiTietToBuomDA : DBConnection
    {
        // hàm tạo
        public ChiTietToBuomDA()
        {
            sqlCommand.Connection = sqlConnection;
        }
        // lấy tất cả
        public DataSet GetAll()
        {
            var ds = new DataSet();
            const string query = "select * from CHITIETTOBUOM";
            if (open())
            {
                sqlDataAdapter = new SqlDataAdapter(query, sqlConnection);
                sqlDataAdapter.Fill(ds);
            }
            close();
            return ds;
        }
        public string GetGiaTien(ChiTietToBuomDTO item)
        {
            var values = string.Empty;
            string query = String.Format("select GiaTienToBuom from CHITIETTOBUOM where MaSoLuong= '{0}'", item.MaSoLuong);
            sqlCommand.CommandText = query;
            try
            {
                open();
                sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader == null)
                {
                    return string.Empty;
                }
                while (sqlDataReader.Read())
                {
                    values = sqlDataReader[0].ToString();
                }
                close();
                return values;
            }
            catch
            {
                return values;
            }
            finally
            {
                if (sqlDataReader != null)
                {
                    sqlDataReader.Close();
                }
                close();
            }
        }
        public int GetID(ChiTietToBuomDTO item)
        {
            string query = String.Format("select MaChiTietToBuom from CHITIETTOBUOM where MaSoLuong='{0}'",item.MaSoLuong);
            var id = 0;
            sqlCommand.CommandText = query;
            try
            {
                open();
                sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader == null)
                {
                    return 0;
                }
                while (sqlDataReader.Read())
                {
                    id = sqlDataReader.GetInt32(0);
                }
                close();
                return id;
            }
            catch
            {
                return 0;
            }
            finally
            {
                if (sqlDataReader != null)
                {
                    sqlDataReader.Close();
                }
                close();
            }
        }
        // thêm 
        public int Add(ChiTietToBuomDTO item)
        {
            var query = String.Format("insert into CHITIETTOBUOM values('{0}','{1}'); select SCOPE_IDENTITY();",
                item.MaSoLuong, item.GiaTienToBuom);
            sqlCommand.CommandText = query;
            try
            {
                open();
                var idRe = -1;
                sqlCommand.CommandText = query;
                var re = sqlCommand.ExecuteScalar().ToString();
                idRe = Int32.Parse(re);
                close();
                return idRe;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }
        // sửa
        public bool Update(ChiTietToBuomDTO item)
        {
            var query = String.Format("Update CHITIETTOBUOM set MaSoLuong = '{0}',GiaTienToBuom='{1}' where MaChiTietToBuom = {2}",
                item.MaSoLuong, item.GiaTienToBuom, item.MaChiTietToBuom);
            sqlCommand.CommandText = query;

            try
            {
                open();
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                close();
                return true;
            }
            catch (SqlException s)
            {
                Console.WriteLine(s.Message);
                return false;
            }
        }
        // xóa 
        public bool Delete(int ma_item)
        {
            var query = "Delete from CHITIETTOBUOM where MaChiTietToBuom = " + ma_item;
            sqlCommand.CommandText = query;
            try
            {
                open();
                sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                close();
                return true;
            }
            catch (SqlException s)
            {
                Console.WriteLine(s.Message);
                return false;
            }
        }
        public string[] getInfo(int machitiettobuom)
        {
            string querySoluongTobuom = String.Format("Select SoLuong,GiaTienToBuom from SOLUONGTOBUOM,CHITIETTOBUOM where CHITIETTOBUOM.MaSoLuong=SOLUONGTOBUOM.MaSoLuong and MaChiTietToBuom='{0}'", machitiettobuom);
           return new MyQueryDA().getArrValues(querySoluongTobuom);
        }
    }
}
